package com.stars.questions.deptQuestion.census.controller;

import com.stars.base.entity.Result;
import com.stars.questions.deptQuestion.census.entity.StatisticConfigDTO;
import com.stars.questions.deptQuestion.census.service.CensusService;
import org.apache.poi.util.StringUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;

@RestController
@RequestMapping("/census")
public class CensusController {
    private final CensusService censusService;

    public CensusController(CensusService censusService) {
        this.censusService = censusService;
    }

    @RequestMapping("/totalData")
    public Result totalData(@RequestBody StatisticConfigDTO dto) {
        Integer year = dto.getYear();
        Integer limit = 1;
        Integer page = dto.getPageSize();
        if(year == null){
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
        }
        return Result.success(censusService.totalData(year.toString(),limit,page));
    }

    @RequestMapping("/getTotalBarData")
    public Result getTotalBarData(@RequestBody StatisticConfigDTO dto) {
        String questionId = dto.getQuestionId();
        return Result.success(censusService.getTotalBarData(questionId));
    }

    @RequestMapping("/totalTopData")
    public Result totalTopData(HttpServletRequest request) {
        String year = request.getParameter("year");
        if(StringUtil.isBlank(year)){
            Calendar calendar = Calendar.getInstance();
            year = String.valueOf(calendar.get(Calendar.YEAR));
        }
        return Result.success(censusService.totalTopData(year));
    }

    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletRequest request , HttpServletResponse response){
        String year = request.getParameter("year");
        if(StringUtil.isBlank(year)){
            Calendar calendar = Calendar.getInstance();
            year = String.valueOf(calendar.get(Calendar.YEAR));
        }
        String quesCount = request.getParameter("quesCount");
        String cStore = request.getParameter("cStore");
        censusService.exportCensusData(response,year,quesCount,cStore);
    }
}
